package com.yanqu.road.server.manager.douluodalu;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuPatronsBattleData;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuTeamData;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuUserData;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

public class CrossDouLuoDaLuContext {

    CrossDouLouDaLuActivity activity = null;

    DouLuoDaLuUserData userData = null;

    DouLuoDaLuTeamData teamData = null;

    DouLuoDaLuPatronsBattleData battleData = null;

    ActivityInfo activityInfo = null;

    int activityId;

    long userId;

    int ret;

    private CrossDouLuoDaLuContext(int activityId, long userId) {
        this.activityId = activityId;
        this.userId = userId;
    }

    public static CrossDouLuoDaLuContext initContext(int activityId, long userId) {
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);

        CrossDouLuoDaLuContext context = new CrossDouLuoDaLuContext(activityId, userId);
        CrossDouLouDaLuActivity douLouDaLuActivity = CrossDouLuoDaLuMgr.getActivityOrNull(activityId, groupId);
        if (douLouDaLuActivity == null) {
            context.ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return context;
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            context.ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return context;
        }
        context.activityInfo = activityInfo;
        context.activity = douLouDaLuActivity;
        DouLuoDaLuUserData userData = douLouDaLuActivity.getAllUserMap().get(userId);
        if(userData == null){
            context.ret = GameErrorCode.E_DOU_LUO_DA_LU_ACTIVITY_USER_NO_INIT;
            return context;
        }
        context.userData = userData;
        return context;
    }

    public CrossDouLuoDaLuContext loadTeamData(){
        if(userData != null){
            if(userData.getTeamId() == 0){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_TEAM_NO_EXIST;
                return this;
            }
            DouLuoDaLuTeamData team = activity.getAllTeamMap().get(userData.getTeamId());
            if(null == team){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_TEAM_NO_EXIST;
                return this;
            }
            teamData = team;
        }
        return this;
    }

    public CrossDouLuoDaLuContext loadBattleData(){
        if(ret == 0 && userData != null){
            if(userData.getBattleData() != null){
                battleData = userData.getBattleData();
                return this;
            }
            battleData = activity.getBattleDataMap().get(userId);
            if(battleData == null){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_ACTIVITY_USER_NO_INIT;
                return this;
            }
            userData.setBattleData(battleData);
        }
        return this;
    }

    public CrossDouLuoDaLuContext loadTeamData(int teamId){
        if(userData != null){
            if(teamId == 0){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_TEAM_NO_EXIST;
                return this;
            }
            DouLuoDaLuTeamData team = activity.getAllTeamMap().get(teamId);
            if(null == team){
                ret = GameErrorCode.E_DOU_LUO_DA_LU_TEAM_NO_EXIST;
                return this;
            }
            teamData = team;
        }
        return this;
    }


    public CrossDouLouDaLuActivity getActivity() {
        return activity;
    }

    public DouLuoDaLuUserData getUserData() {
        return userData;
    }

    public DouLuoDaLuPatronsBattleData getBattleData() {
        return battleData;
    }

    public DouLuoDaLuTeamData getTeamData() {
        return teamData;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getActivityId() {
        return activityId;
    }

    public long getUserId() {
        return userId;
    }

    public int getRet() {
        return ret;
    }

    public String getUnionUid() {
        return CrossDouLuoDaLuMgr.getUserActivityUnionUid(activityId, userId);
    }
}
